package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.HomeBannerCate;
import com.dingtao.common.bean.HomeCateTwo;
import com.dingtao.common.bean.Order;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.HomeBannerCatePresenter;
import com.example.peibei.service.presenter.HomeOrderFollowPresenter;
import com.example.peibei.service.presenter.HomeOrderPresenter;
import com.example.peibei.ui.activity.CateOrderActivity;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.activity.PersonInfoActivityTwo;
import com.example.peibei.ui.activity.PersonalInfoActivity;
import com.example.peibei.ui.activity.SearchActivity;
import com.example.peibei.ui.adapter.HomeCateAdapter;
import com.example.peibei.ui.adapter.HomeOrderListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.example.peibei.ui.weight.HorizontalPageLayoutManager;
import com.example.peibei.ui.weight.PagingScrollHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.relex.circleindicator.CircleIndicator;

public class HomeChildFollowFragment extends WDFragment {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    String type = "";
    private HomeOrderListAdapter homeOrderListAdapter;
    private HomeOrderFollowPresenter homeOrderPresenter;
    private SPUtils spUtils;
    private String token;
    private long mid;

    public HomeChildFollowFragment setType(String type) {
        this.type = type;
        return this;
    }

    @Override
    public String getPageName() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_child;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);

        homeOrderListAdapter = new HomeOrderListAdapter();
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Order.RecordsDTO order = homeOrderListAdapter.getItem(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle = new Bundle();
            bundle.putString("publishOrderId", publishOrderId);
            intent(OrderDetailActivity.class, bundle);
        });
        homeOrderListAdapter.addChildClickViewIds(R.id.iv_head);
        homeOrderListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.iv_head) {
                Order.RecordsDTO order = homeOrderListAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString("mid", order.getMid());
                intent(PersonInfoActivityTwo.class, bundle);
            }
        });

        refreshLayout.setOnRefreshListener(refreshLayout -> onRefreshStart());
        refreshLayout.setOnLoadMoreListener(refreshLayout -> onLoadMoreStart());

        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(homeOrderListAdapter);

        //获取数据
        homeOrderPresenter = new HomeOrderFollowPresenter(new OrderCall());
        View head = LayoutInflater.from(getContext()).inflate(R.layout.head_home_child_follow,null,false);
        homeOrderListAdapter.setHeaderView(head);
        head.findViewById(R.id.rl_search).setOnClickListener(v -> intent(SearchActivity.class));
    }

    @Override
    public void onResume() {
        super.onResume();
        token = spUtils.getString(SpConstant.USER_TOKEN);
        mid = spUtils.getLong(SpConstant.USER_MID);
        onRefreshStart();
    }

    public void onRefreshStart() {
        if (homeOrderPresenter.isRunning()) {
            refreshLayout.finishRefresh();
            return;
        }
        homeOrderPresenter.reqeust(true, mid + "", "Bearer " + token);

    }

    public void onLoadMoreStart() {
        if (homeOrderPresenter.isRunning()) {
            refreshLayout.finishLoadMore();
            return;
        }
        homeOrderPresenter.reqeust(false, mid + "", "Bearer " + token);
    }

    class OrderCall implements DataCall<Order> {

        View view;
        @Override
        public void success(Order data, Object... args) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            //添加列表并刷新
            if (homeOrderPresenter.getPage() == 1) {
                homeOrderListAdapter.setNewInstance(data.getRecords());
            }else {
                homeOrderListAdapter.addData(data.getRecords());
            }
            if (homeOrderListAdapter.getData().isEmpty()){
                view = LayoutInflater.from(getContext()).inflate(R.layout.empty_home_child,null,false);
                homeOrderListAdapter.setFooterView(view);
            }else {
                homeOrderListAdapter.removeFooterView(view);
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }
}
